basic.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="container main">
  3. <view class="one">
  4. <form @submit="formSubmit">
  5. <view class="value other" style="display: none;">
  6. <view class="title">id</view>
  7. <view class="label">
  8. <input name="id" class="input" :value="form.id" placeholder="请输入id" />
  9. </view>
  10. </view>
  11. <view class="value other">
  12. <view class="title">账号</view>
  13. <view class="label">
  14. <text v-if="form.account">{{form.account}}</text>
  15. <input v-else name="account" class="input" :value="form.account" placeholder="请输入账号" />
  16. </view>
  17. </view>
  18. <view class="value other margin">
  19. <view class="title">昵称</view>
  20. <view class="label">
  21. <input name="nick_name" class="input" :value="form.nick_name" placeholder="请输入昵称" />
  22. <span v-if="errors.nick_name" class="error-message">{{ errors.nick_name }}</span>
  23. </view>
  24. </view>
  25. <view class="value other">
  26. <view class="title">所属产业</view>
  27. <view class="label">
  28. <uni-data-checkbox mode="button" multiple v-model="form.industry" :localdata="industryList"
  29. :map="{text:'title',value:'title'}"></uni-data-checkbox>
  30. <span v-if="errors.industry" class="error-message">{{ errors.industry }}</span>
  31. </view>
  32. </view>
  33. <view class="value other">
  34. <view class="title">性别</view>
  35. <view class="label">
  36. <picker class="picker" mode="selector" :range="genderList" @change="genderChange"
  37. range-key="label">
  38. <view>{{gender_name||'请选择性别'}}</view>
  39. </picker>
  40. <text class="iconfont icon-dayuhao"></text>
  41. <span v-if="errors.gender" class="error-message">{{ errors.gender }}</span>
  42. </view>
  43. </view>
  44. <view class="value other">
  45. <view class="title">手机号</view>
  46. <view class="label">
  47. <input name="phone" class="input" :value="form.phone" placeholder="请输入手机号" />
  48. <span v-if="errors.phone" class="error-message">{{ errors.phone }}</span>
  49. </view>
  50. </view>
  51. <view class="value other">
  52. <view class="title">电子邮箱</view>
  53. <view class="label">
  54. <input name="email" class="input" :value="form.email" placeholder="请输入电子邮箱" />
  55. <span v-if="errors.email" class="error-message">{{ errors.email }}</span>
  56. </view>
  57. </view>
  58. <view class="button">
  59. <button type="warn" form-type="submit">保存</button>
  60. </view>
  61. </form>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. export default {
  67. components: {},
  68. data() {
  69. return {
  70. openid: '',
  71. user: {},
  72. form: {
  73. industry: '',
  74. },
  75. gender_name: '',
  76. // 字典表
  77. genderList: [],
  78. industryList: [],
  79. errors: {}
  80. }
  81. },
  82. onLoad: async function(e) {
  83. const that = this;
  84. await that.searchOpenids();
  85. await that.searchToken();
  86. await that.searchOther();
  87. await that.search();
  88. },
  89. methods: {
  90. async searchOpenids() {
  91. const that = this;
  92. uni.getStorage({
  93. key: 'openid',
  94. success: function(res) {
  95. that.$set(that, `openid`, res.data);
  96. },
  97. fail: function(err) {
  98. uni.login({
  99. success: async function(res) {
  100. if (res.code) {
  101. const aee = await that.$app('/wechat/api/login/app',
  102. 'GET', {
  103. js_code: res.code,
  104. config: that.$config.wx_projectkey
  105. })
  106. if (aee.errcode == '0') {
  107. uni.setStorage({
  108. key: "openid",
  109. data: aee.data.openid
  110. })
  111. that.$set(that, `openid`, aee.data.openid);
  112. } else {
  113. uni.showToast({
  114. title: aee.errmsg,
  115. icon: 'none'
  116. })
  117. }
  118. } else {
  119. uni.showToast({
  120. title: res.errMsg,
  121. icon: 'none'
  122. })
  123. }
  124. }
  125. });
  126. }
  127. })
  128. },
  129. // 用户信息
  130. searchToken() {
  131. const that = this;
  132. try {
  133. const res = uni.getStorageSync('token');
  134. if (res) {
  135. const user = that.$jwt(res);
  136. that.$set(that, `user`, user);
  137. }
  138. } catch (e) {}
  139. },
  140. async searchOther() {
  141. const that = this;
  142. let res;
  143. // 查询性别
  144. res = await that.$api(`/dictData`, 'GET', {
  145. code: 'gender',
  146. is_use: '0',
  147. })
  148. if (res.errcode == '0') that.$set(that, `genderList`, res.data);
  149. // 查询所属产业
  150. res = await that.$api(`/sector`, 'GET', {
  151. is_use: '0',
  152. })
  153. if (res.errcode == '0') that.$set(that, `industryList`, res.data);
  154. },
  155. // 查询
  156. async search() {
  157. const that = this;
  158. if (that.user && that.user.id) {
  159. let res;
  160. res = await that.$api(`/user/${that.user.id}`, 'GET', {})
  161. if (res.errcode == '0') {
  162. that.$set(that, `form`, res.data)
  163. if (res.data.gender) {
  164. let data = that.genderList.find(i => i.value == res.data.gender);
  165. if (data) that.$set(that, `gender_name`, data.label)
  166. }
  167. } else {
  168. uni.showToast({
  169. title: res.errmsg,
  170. });
  171. }
  172. }
  173. },
  174. // 性别选择
  175. genderChange(e) {
  176. const that = this;
  177. let data = that.genderList.find(i => i.value == e.detail.value);
  178. if (data) {
  179. that.$set(that.form, `gender`, data.value)
  180. that.$set(that, `gender_name`, data.label)
  181. }
  182. },
  183. // 自定义的验证函数
  184. validateObject(obj) {
  185. const errors = {};
  186. if (!obj.nick_name || obj.nick_name.trim() === '') {
  187. errors.nick_name = '请填写昵称!';
  188. }
  189. if (!obj.industry || obj.industry.trim() === '') {
  190. errors.industry = '请选择所属产业!';
  191. }
  192. if (!obj.gender || obj.gender.trim() === '') {
  193. errors.gender = '请选择性别!';
  194. }
  195. if (!obj.phone || obj.phone.trim() === '') {
  196. errors.phone = '请填写手机号!';
  197. } else {
  198. const regex = /^1[3456789]\d{9}$/;
  199. if (!regex.test(obj.phone)) errors.phone = '请填写正确的手机号码!';
  200. }
  201. if (!obj.email || obj.email.trim() === '') {
  202. errors.email = '请填写电子邮箱!';
  203. } else {
  204. const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
  205. if (!regex.test(obj.email)) errors.email = '请填写正确的电子邮箱!';
  206. }
  207. // 如果有错误,返回错误对象
  208. if (Object.keys(errors).length > 0) {
  209. return errors;
  210. }
  211. // 如果没有错误,返回null或undefined
  212. return null;
  213. },
  214. async formSubmit(e) {
  215. const that = this;
  216. const form = that.form
  217. const data = e.detail.value;
  218. if (!form.openid) data.openid = that.openid
  219. data.gender = form.gender
  220. data.industry = form.industry
  221. const errorsInfo = await that.validateObject(data);
  222. // 检查是否有错误
  223. if (errorsInfo) {
  224. that.$set(that, `errors`, errorsInfo)
  225. // 遍历错误对象并显示错误信息
  226. for (const key in errorsInfo) {
  227. if (errorsInfo.hasOwnProperty(key)) {
  228. console.error(`${key} 错误: ${errorsInfo[key]}`);
  229. }
  230. }
  231. } else {
  232. that.$set(that, `errors`, {})
  233. const res = await that.$api(`/user/${that.user.id}`, 'POST', data);
  234. if (res.errcode == '0') that.search();
  235. else {
  236. uni.showToast({
  237. title: res.errmsg,
  238. });
  239. }
  240. }
  241. },
  242. }
  243. }
  244. </script>
  245. <style lang="scss" scoped>
  246. .main {
  247. background-color: var(--footColor);
  248. .one {
  249. .icon {
  250. padding: 2vw;
  251. }
  252. .margin {
  253. margin: 3vw 0 0 0;
  254. }
  255. .other {
  256. padding: 3vw 2vw;
  257. border-bottom: 1px solid var(--footColor);
  258. .checkbox-label {
  259. display: flex;
  260. flex-wrap: wrap;
  261. }
  262. }
  263. .value {
  264. display: flex;
  265. justify-content: space-between;
  266. align-items: center;
  267. background-color: var(--mainColor);
  268. .title {
  269. min-width: 25%;
  270. }
  271. .label {
  272. text-align: right;
  273. .error-message {
  274. margin: 5px 0 0 0;
  275. color: var(--fF0Color);
  276. font-size: var(--font12Size);
  277. }
  278. }
  279. }
  280. .remark {
  281. padding: 4vw 2vw;
  282. text-align: center;
  283. color: var(--f99Color);
  284. font-size: var(--font18Size);
  285. }
  286. .button {
  287. margin: 2vw 0 0 0;
  288. padding: 2vw;
  289. text-align: center;
  290. button {
  291. background-color: var(--f3CColor);
  292. font-size: var(--font14Size);
  293. border-radius: 2vw;
  294. }
  295. }
  296. }
  297. }
  298. </style>