index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="content">
  3. <view class="logo">
  4. <image src="../../static/logo.png" mode=""></image>
  5. </view>
  6. <h3>版本:V{{vuex_version}}</h3>
  7. <u-button v-if="approval === false" class="butto_N" open-type="getUserInfo" lang="zh_CN"
  8. @getuserinfo="appLoginWx" type="primary" size="default">微信授权登录</u-button>
  9. <!-- <u-button v-if="approval === true" class="butto_N" open-type="getUserInfo" lang="zh_CN" @getuserinfo="appLoginWx" type="primary" size="default">授权手机号</u-button> -->
  10. <u-button v-if="approval === true" class="butto_N" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"
  11. lang="zh_CN" type="primary" size="default">授权手机号</u-button>
  12. <view style="position:absolute; bottom:20rpx;font-size: 20rpx;">说明:本小程序仅限青岛靖润科技物流有限公司内部员工使用</view>
  13. <u-toast ref="uToast" />
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. request
  19. } from '@/common/request/request'
  20. import {
  21. mapState,
  22. mapMutations
  23. } from 'vuex'
  24. export default {
  25. data() {
  26. return {
  27. approval: false,
  28. time: '',
  29. code: ''
  30. }
  31. },
  32. onLoad() {
  33. },
  34. created() {
  35. // uni.redirectTo({
  36. // url: '../home/myorder/index'
  37. // });
  38. // #ifdef MP-WEIXIN
  39. if (wx.hideHomeButton) {
  40. wx.hideHomeButton();
  41. }
  42. // #endif
  43. this.testing()
  44. },
  45. computed: {
  46. ...mapState(['hasLogin'])
  47. },
  48. methods: {
  49. ...mapMutations(['login']),
  50. onGetPhoneNumber(e) {
  51. console.log(e)
  52. let this_ = this
  53. if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
  54. //拒绝授权
  55. this.$refs.uToast.show({
  56. title: '授权失败',
  57. })
  58. } else {
  59. console.log(this_.code)
  60. //允许授权
  61. wx.getUserInfo({
  62. success: function(s) {
  63. console.log(s)
  64. request({
  65. url: '/wechat/programLogin',
  66. method: 'post',
  67. data: {
  68. code: this_.code,
  69. iv: e.detail.iv,
  70. encryptedIv: s.iv,
  71. phonenumber: e.detail.encryptedData,
  72. encryptedData:s.encryptedData
  73. }
  74. }).then(res => {
  75. console.log(res)
  76. if (res.data.code == 200) {
  77. this_.$refs.uToast.show({
  78. title: '登录成功',
  79. })
  80. uni.setStorage({
  81. key: 'vuex_token',
  82. data: res.data.data.token,
  83. success(res) {
  84. console.log("成功")
  85. }
  86. })
  87. let time = new Date(new Date().setDate(new Date().getDate() + 6))
  88. // console.log(time.getTime())
  89. uni.setStorage({
  90. key: 'effective_time',
  91. data: time.getTime(),
  92. success(res) {
  93. console.log("成功")
  94. }
  95. })
  96. uni.setStorage({
  97. key: 'cell_phone',
  98. data: res.data.data.phonenumber,
  99. success(res) {
  100. // console.log(res)
  101. console.log("成功")
  102. }
  103. })
  104. uni.redirectTo({
  105. url: '../home/index'
  106. });
  107. } else if (res.data.code === 500) {
  108. this_.$refs.uToast.show({
  109. title: res.data.msg,
  110. })
  111. }
  112. }).catch(err => {
  113. console.log(err)
  114. this_.$refs.uToast.show({
  115. title: err,
  116. })
  117. })
  118. }
  119. })
  120. }
  121. },
  122. appLoginWx() {
  123. let this_ = this
  124. // #ifdef MP-WEIXIN
  125. uni.getProvider({
  126. service: 'oauth',
  127. success: function(res) {
  128. if (~res.provider.indexOf('weixin')) {
  129. uni.login({
  130. provider: 'weixin',
  131. success: (res2) => {
  132. uni.getUserInfo({
  133. provider: 'weixin',
  134. success: (info) => { //这里请求接口
  135. this_.code = res2.code
  136. this_.approval = true
  137. console.log(res)
  138. },
  139. fail: () => {
  140. uni.showToast({
  141. title: "微信登录授权失败",
  142. icon: "none"
  143. });
  144. }
  145. })
  146. },
  147. fail: () => {
  148. uni.showToast({
  149. title: "微信登录授权失败",
  150. icon: "none"
  151. });
  152. }
  153. })
  154. } else {
  155. uni.showToast({
  156. title: '请先安装微信或升级版本',
  157. icon: "none"
  158. });
  159. }
  160. }
  161. });
  162. //#endif
  163. },
  164. testing() {
  165. let this_ = this
  166. // #ifdef MP-WEIXIN
  167. uni.getSetting({
  168. success(res) {
  169. if (!res.authSetting['scope.userInfo']) {
  170. //这里调用授权
  171. this_.approval = false
  172. } else {
  173. //用户已经授权过了
  174. console.log("当前已授权");
  175. this_.appLoginWx()
  176. let time = new Date().getTime()
  177. this_.approval = true
  178. console.log(uni.getStorageSync('vuex_token'))
  179. console.log(uni.getStorageSync('cell_phone'))
  180. console.log(uni.getStorageSync('effective_time'))
  181. // request({
  182. // url: '/wechat/programLogin',
  183. // method: 'post',
  184. // data: {
  185. // phonenumber: uni.getStorageSync('cell_phone'),
  186. // token: uni.getStorageSync('vuex_token')
  187. // }
  188. // }).then(res => {
  189. // console.log(res)
  190. // uni.setStorage({
  191. // key: 'vuex_token',
  192. // data: res.data.data.token,
  193. // success(es) {
  194. // console.log(res)
  195. // console.log("成功")
  196. // }
  197. // })
  198. // let time = new Date(new Date().setDate(Date().getDate() + 6))
  199. // console.log(time.getTime())
  200. // uni.setStorage({
  201. // key: 'effective_time',
  202. // data: time.getTime(),
  203. // success(res) {
  204. // console.log(res)
  205. // }
  206. // })
  207. // uni.redirectTo({
  208. // url: '../home/index'
  209. // });
  210. // }).catch(err => {
  211. // console.log(err)
  212. // })
  213. }
  214. }
  215. })
  216. //#endif
  217. },
  218. }
  219. }
  220. </script>
  221. <style scoped lang="scss">
  222. .butto_N {
  223. width: 80%;
  224. margin-top: 100px;
  225. }
  226. .text {
  227. margin-top: 20px;
  228. view {
  229. margin-bottom: 20rpx;
  230. }
  231. }
  232. .logo {
  233. width: 200rpx;
  234. height: 200rpx;
  235. border-radius: 40rpx;
  236. background-color: #3481f6;
  237. margin-top: 100px;
  238. margin-bottom: 20px;
  239. image {
  240. width: 200rpx;
  241. height: 200rpx;
  242. }
  243. }
  244. .content {
  245. display: flex;
  246. flex-direction: column;
  247. align-items: center;
  248. justify-content: center;
  249. }
  250. .logo {
  251. height: 200rpx;
  252. width: 200rpx;
  253. margin-top: 200rpx;
  254. margin-left: auto;
  255. margin-right: auto;
  256. margin-bottom: 50rpx;
  257. }
  258. .text-area {
  259. display: flex;
  260. justify-content: center;
  261. }
  262. .title {
  263. font-size: 36rpx;
  264. color: #8f8f94;
  265. }
  266. </style>