index.vue 6.9 KB

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