index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="auth">
  3. <image src="/static/logo.png" mode="aspectFill"></image>
  4. <text class="margin-top-3 auth-title">欢迎使用空i企小程序</text>
  5. <text class="margin-top-3 auth-content">此页面是微信授权页面,授权之后你可以获取更优质的服务,您的隐私将会受到保护</text>
  6. <view class="margin-top-3">
  7. <button type='default' class="refuse" @click="refuseBtn">暂不授权</button>
  8. <button type='primary' class="allow" @click="getUserInfo()" v-if="!isAuth">登录授权</button>
  9. <button type="primary" class="allow" open-type="getPhoneNumber" style="font-size: 12px;"
  10. @getphonenumber="getPhoneNumber" v-if="isAuth && !isNeedPhone">手机号码授权</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import md5 from '@/common/md5.js';
  16. export default {
  17. data(){
  18. return {
  19. iv:'',
  20. encryptedData:'',
  21. isNeedPhone:getApp().globalData.user_phone,
  22. isAuth:getApp().globalData.isAuth
  23. }
  24. },
  25. onLoad() {
  26. this.getUserInfoData()
  27. },
  28. methods:{
  29. getUserInfoData(){
  30. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  31. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&action=info_by_openid&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  32. uni.request({
  33. url:url,
  34. method: 'POST',
  35. header: {
  36. 'content-type': 'application/x-www-form-urlencoded'
  37. },
  38. data: {
  39. openId:getApp().globalData.open_id
  40. },
  41. success: (res) => {
  42. if(res.data.code === 200){
  43. if(res.data.data.nickname){
  44. getApp().globalData.isAuth = true;
  45. getApp().globalData.user_phone = false;
  46. this.isAuth = true;
  47. this.isNeedPhone = false;
  48. }else{
  49. getApp().globalData.isAuth = false;
  50. this.isAuth = false;
  51. }
  52. }
  53. },
  54. fail: () => {
  55. console.log("连接失败");
  56. }
  57. });
  58. },
  59. getUserInfo() {
  60. uni.getUserProfile({
  61. desc:'登录',
  62. success:(res)=> {
  63. getApp().globalData.user_headUrl = res.userInfo.avatarUrl;
  64. getApp().globalData.user_name = res.userInfo.nickName;
  65. this.iv = res.iv;
  66. this.encryptedData = res.encryptedData;
  67. this.isAuth = true;
  68. getApp().globalData.isAuth = true;
  69. this.loginUserInfo()
  70. },
  71. fail:(err)=> {
  72. getApp().globalData.isAuth = false;
  73. this.isAuth = false;
  74. }
  75. })
  76. // if (e.detail.errMsg == "getUserInfo:ok") {
  77. // getApp().globalData.user_headUrl = e.detail.userInfo.avatarUrl;
  78. // getApp().globalData.user_name = e.detail.userInfo.nickName;
  79. // this.iv = e.detail.iv;
  80. // this.encryptedData = e.detail.encryptedData;
  81. // console.log(e.detail)
  82. // //this.loginUserInfo()
  83. // } else {
  84. // console.log("用户信息授权失败");
  85. // getApp().globalData.isAuth = false;
  86. // }
  87. },
  88. getPhoneNumber(e){
  89. let that = this;
  90. if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
  91. console.log(e.detail)
  92. that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
  93. } else { //
  94. that.isNeedPhone = false;
  95. }
  96. },
  97. phoneRequest(myIv,myEncryptedData,sKey){
  98. let that = this;
  99. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  100. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=phone&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  101. uni.request({
  102. url:url,
  103. method: 'POST',
  104. header: {
  105. 'content-type': 'application/x-www-form-urlencoded'
  106. },
  107. data: {
  108. iv:myIv,
  109. sessionKey:sKey,
  110. encryptedData:myEncryptedData,
  111. openId:getApp().globalData.open_id
  112. },
  113. success: (res) => {
  114. console.log(res)
  115. if(res.data.code === 200){
  116. getApp().globalData.user_phone = res.data.data;
  117. setTimeout(()=>{
  118. that.refuseBtn();
  119. },300)
  120. }
  121. },
  122. fail: () => {
  123. console.log("连接失败");
  124. }
  125. });
  126. },
  127. refuseBtn(){
  128. uni.navigateBack({
  129. });
  130. },
  131. loginUserInfo(){
  132. let that = this;
  133. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  134. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=update&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  135. uni.request({
  136. url:url, //需要设置为全局
  137. method: 'POST',
  138. header: {
  139. 'content-type': 'application/x-www-form-urlencoded'
  140. },
  141. data: {
  142. openId:getApp().globalData.open_id,
  143. nickname:getApp().globalData.user_name,
  144. headimg:getApp().globalData.user_headUrl,
  145. },
  146. success: res => {
  147. if(res.data.code === 200){
  148. console.log(res.data)
  149. // getApp().globalData.user_id = res.data.msg.id;
  150. // getApp().globalData.open_id = res.data.msg.openid;
  151. // getApp().globalData.isAuth = true;
  152. // getApp().globalData.user_headUrl = res.data.msg.headimg;
  153. // getApp().globalData.user_name = res.data.msg.name;
  154. // getApp().globalData.user_status = res.data.msg.status;
  155. // getApp().globalData.user_phone = res.data.msg.phone;
  156. // that.isNeedPhone = res.data.msg.phone;
  157. // that.isAuth = getApp().globalData.isAuth;
  158. }
  159. else {
  160. uni.showToast({
  161. title: res.data.msg,
  162. icon: 'none',
  163. duration:2000
  164. });
  165. }
  166. // uni.navigateBack({
  167. // });
  168. }
  169. });
  170. },
  171. }
  172. }
  173. </script>
  174. <style>
  175. .auth{
  176. margin-top: 0;
  177. text-align: center;
  178. display: flex;
  179. flex-direction: column;
  180. justify-content: center;
  181. align-items: center;
  182. padding: 100upx;
  183. vertical-align: middle;
  184. }
  185. .auth image{
  186. width: 200upx;
  187. height: 200upx;
  188. }
  189. .auth view {
  190. display: flex;
  191. width: 490upx;
  192. justify-content: space-between;
  193. }
  194. .auth-title {
  195. font-size: 32upx;
  196. }
  197. .auth-content {
  198. font-size: 26upx;
  199. color: #a7aaa9;
  200. }
  201. .allow {
  202. background-color: #27BCEF;
  203. margin: 20rpx 0 200rpx;
  204. text-align: center;
  205. vertical-align: middle;
  206. touch-action: manipulation;
  207. cursor: pointer;
  208. background-image: none;
  209. white-space: nowrap;
  210. user-select: none;
  211. font-size: 14px;
  212. border: 0 !important;
  213. position: relative;
  214. text-decoration: none;
  215. height: 44px;
  216. width: 250rpx;
  217. line-height: 44px;
  218. box-shadow: inset 0 0 0 1px #27BCEF;
  219. background: #fff !important;
  220. color: #27BCEF !important;
  221. display: inline-block;
  222. border-radius: 10rpx;
  223. }
  224. .refuse {
  225. background-color: #19be6b;
  226. margin: 20rpx 20rpx 200rpx 20rpx;
  227. text-align: center;
  228. vertical-align: middle;
  229. touch-action: manipulation;
  230. cursor: pointer;
  231. background-image: none;
  232. white-space: nowrap;
  233. user-select: none;
  234. font-size: 14px;
  235. border: 0 !important;
  236. position: relative;
  237. text-decoration: none;
  238. height: 44px;
  239. width: 250rpx;
  240. line-height: 44px;
  241. box-shadow: inset 0 0 0 1px #8a8a8a;
  242. background: #fff !important;
  243. color: #8a8a8a !important;
  244. display: inline-block;
  245. border-radius: 10rpx;
  246. }
  247. .margin-top-3{
  248. margin-top: 10%;
  249. }
  250. </style>