图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.6 KiB

1 month ago
  1. <template>
  2. <view style="background-color: #fff; height: calc(100vh);">
  3. <view class="user-info-section">
  4. <!-- 点击获取头像 -->
  5. <button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" class="avatar-btn">
  6. <image v-if="avatarUrl" :src="avatarUrl" class="avatar-img"></image>
  7. <image v-else src="@/static/images/avatar.png" class="avatar-img"></image>
  8. <text class="tip-text">点击选择头像</text>
  9. </button>
  10. </view>
  11. <view class="form-box">
  12. <view class="item">
  13. <uni-icons class="form-icon" type="person" size="24"></uni-icons>
  14. <view class="uni-input-wrapper">
  15. <input class="input" type="nickname" v-model="nickName" @blur="onNicknameBlur" placeholder="请输入昵称" @input="clearInput" />
  16. <uni-icons class="clear-icon" v-if="showClearIcon" @click="clearIcon" type="close" size="20"></uni-icons>
  17. </view>
  18. </view>
  19. <view class="item">
  20. <uni-icons class="form-icon" custom-prefix="iconfont" type="icon-duzhezheng" size="24"></uni-icons>
  21. <view class="uni-input-wrapper">
  22. <input class="input" placeholder="请输入读者证号" v-model="queryvalue" @input="clearInput" />
  23. <uni-icons class="clear-icon" v-if="showClearIcon" @click="clearIcon" type="close" size="20"></uni-icons>
  24. </view>
  25. </view>
  26. <view class="item">
  27. <uni-icons class="form-icon" type="locked" size="24"></uni-icons>
  28. <input class="input" placeholder="请输入密码" :password="!showPwd" v-model="rdpasswd" />
  29. <uni-icons class="form-right-icon" :type="showPwd ? 'eye-slash' : 'eye'" size="20"
  30. @click="togglePwd"></uni-icons>
  31. </view>
  32. <button class="login-btn" type="primary" @click="submit">绑定</button>
  33. </view>
  34. <view class="tips">
  35. 温馨提示<br />
  36. 1密码默认为 <text style="color:#e74c3c">身份证后6位</text>如果身份证号最后一位为XX需要大写;
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { FetchInitScreenSetting, FetchReaderList } from '@/api/user';
  42. const TOKEN_KEY = 'token';
  43. const USER_KEY = 'user-info';
  44. export default {
  45. data() {
  46. return {
  47. queryvalue: '',
  48. rdpasswd: '',
  49. showPwd: false,
  50. screenConfig: {},
  51. avatarUrl: '',
  52. nickName: '',
  53. showClearIcon: false,
  54. };
  55. },
  56. onLoad() {
  57. this.getScreenSetting();
  58. },
  59. methods: {
  60. // 获取微信头像
  61. onChooseAvatar(e) {
  62. console.log('获取微信头像',e)
  63. this.avatarUrl = e.detail.avatarUrl;
  64. },
  65. onNicknameBlur(e) {
  66. this.nickName = e.detail.value.trim()
  67. },
  68. clearInput(event) {
  69. this.queryvalue = event.detail.value;
  70. if (event.detail.value.length > 0) {
  71. this.showClearIcon = true;
  72. } else {
  73. this.showClearIcon = false;
  74. }
  75. },
  76. clearIcon() {
  77. this.queryvalue = '';
  78. this.showClearIcon = false;
  79. },
  80. // 切换密码显隐
  81. togglePwd() {
  82. this.showPwd = !this.showPwd;
  83. },
  84. async getScreenSetting() {
  85. try {
  86. const res = await FetchInitScreenSetting({ libcode: '1201' });
  87. const data = res.data;
  88. this.screenConfig = {
  89. thirdUrl: data.open_lib_http?.context || '',
  90. thirdAppid: data.open_lib_appId?.context || '',
  91. thirdSecret: data.open_lib_secret?.context || '',
  92. sm4Key: data.sm4_key?.context || ''
  93. };
  94. } catch (err) {
  95. console.error('获取配置失败:', err);
  96. }
  97. },
  98. async submit() {
  99. // if (!this.avatarUrl) {
  100. // uni.showToast({ title: '请选择头像', icon: 'none' });
  101. // return;
  102. // }
  103. // if (!this.nickName) {
  104. // uni.showToast({ title: '请输入昵称', icon: 'none' });
  105. // return;
  106. // }
  107. if (!this.queryvalue || !this.rdpasswd) {
  108. uni.showToast({ title: '请输入读者证号和密码', icon: 'none' });
  109. return;
  110. }
  111. uni.showLoading({ title: '绑定中...' });
  112. try {
  113. const params = {
  114. ...this.screenConfig,
  115. selecttype: 'rdid',
  116. queryvalue: this.queryvalue,
  117. rdpasswd: this.rdpasswd,
  118. };
  119. const res = await FetchReaderList(params);
  120. let result = {};
  121. try {
  122. result = JSON.parse(res.data);
  123. } catch (e) {
  124. uni.hideLoading();
  125. uni.showToast({ title: '数据解析失败', icon: 'none' });
  126. return;
  127. }
  128. // 2. 统一提取错误信息(兼容大小写、各种格式)
  129. let errMsg = '';
  130. // 兼容小写 messagelist
  131. if (result.messagelist && result.messagelist.length > 0) {
  132. const item = result.messagelist[0];
  133. errMsg = item.message || Object.values(item)[0] || '绑定失败';
  134. }
  135. // 兼容大写 messageList
  136. if (result.messageList && result.messageList.length > 0) {
  137. const item = result.messageList[0];
  138. errMsg = item.message || Object.values(item)[0] || '绑定失败';
  139. }
  140. // 3. 真正的成功判断:必须 success=true 并且 pagedata 有值
  141. const realSuccess = result.success === true && result.pagedata && result.pagedata.length > 0;
  142. console.log('this.avatarUrl',this.avatarUrl)
  143. if (realSuccess) {
  144. const loginRes = {
  145. token: 'reader-token-' + Date.now(),
  146. user: {
  147. nickName: this.nickName || '小图',
  148. avatarUrl: this.avatarUrl,
  149. cardNo: this.queryvalue
  150. }
  151. };
  152. uni.setStorageSync(TOKEN_KEY, loginRes.token);
  153. uni.setStorageSync(USER_KEY, loginRes.user);
  154. uni.hideLoading();
  155. uni.showToast({ title: '绑定成功', icon: 'success' });
  156. setTimeout(() => {
  157. uni.navigateBack();
  158. }, 1500);
  159. } else {
  160. uni.hideLoading();
  161. uni.showToast({
  162. title: errMsg || '读者证或密码错误',
  163. icon: 'none'
  164. });
  165. }
  166. } catch (err) {
  167. uni.hideLoading();
  168. uni.showToast({ title: '网络异常', icon: 'none' });
  169. }
  170. }
  171. }
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. .user-info-section {
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. padding: 30px 0 0 0;
  180. .avatar-btn {
  181. background: transparent;
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. &::after{
  186. border: none !important;
  187. }
  188. .avatar-img {
  189. width: 60px;
  190. height: 60px;
  191. border-radius: 50%;
  192. }
  193. .tip-text {
  194. font-size: 12px;
  195. color: #999;
  196. }
  197. }
  198. // .nickname-box {
  199. // width: 80%;
  200. // margin-top: 10px;
  201. // .nickname-input {
  202. // width: 100%;
  203. // padding: 10px 0;
  204. // border-bottom: 1px solid #eee;
  205. // text-align: center;
  206. // font-size: 14px;
  207. // }
  208. // }
  209. }
  210. .form-box {
  211. padding: 20px 15px;
  212. .item {
  213. width: 100%;
  214. min-height: 40px;
  215. border-radius: 20px;
  216. background-color: #f7f7f7;
  217. display: flex;
  218. align-items: center;
  219. margin-bottom: 15px;
  220. .input {
  221. flex: 1;
  222. padding: 10px;
  223. font-size: 14px;
  224. }
  225. }
  226. .uni-input-wrapper{
  227. flex: 1;
  228. display: flex;
  229. align-items: center;
  230. .input {
  231. flex: 1;
  232. }
  233. .clear-icon{
  234. padding: 0 10px;
  235. }
  236. }
  237. }
  238. .login-btn {
  239. margin-top: 10px;
  240. background-color: #01a4fe !important;
  241. border-radius: 20px;
  242. font-size: 16px;
  243. }
  244. .tips {
  245. margin: 30px 20px;
  246. font-size: 12px;
  247. color: #333;
  248. line-height: 20px;
  249. }
  250. .form-icon {
  251. ::v-deep .uni-icons {
  252. margin-left: 10px;
  253. color: #01a4fe !important;
  254. }
  255. }
  256. .form-right-icon {
  257. ::v-deep .uni-icons {
  258. margin-right: 10px;
  259. }
  260. }
  261. </style>