index.vue 7.8 KB

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