Account.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace app\abutment\logic;
  3. use app\abutment\model\SupplierUser as SupplierUserModel;
  4. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  5. use app\abutment\model\SupplierUser;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use think\helper\Str;
  11. //供应商账号管理
  12. class Account
  13. {
  14. //登录
  15. public static function login(array $param = [])
  16. {
  17. $db = new SupplierUserModel();
  18. $res = $db->where([
  19. 'is_del' => $db::$is_del_normal,
  20. 'mobile' => $param['mobile']
  21. ])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($res)) return json_show(1004, '该账号不存在');
  25. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  26. $password = get_encryption_password($param['password'], $res['salt']);
  27. if ($password['password'] != $res['password']) return json_show(1004, '密码错误');
  28. //更新token
  29. $token = Str::random(40, -1);
  30. $expire_int = mt_rand(3600, 7200);
  31. $expire_time = date('Y-m-d H:i:s', time() + $expire_int);
  32. $rs = $db
  33. ->where($db->getPk(), $res[$db->getPk()])
  34. ->save(['token' => $token, 'expire_time' => $expire_time]);
  35. if (!$rs) return json_show(1005, '更新账号token信息失败');
  36. return json_show(0, '登录成功', ['token' => $token, 'expire_time' => $expire_time]);
  37. }
  38. //获取用户信息
  39. public static function getUserInfo(string $token = '')
  40. {
  41. $db = new SupplierUserModel();
  42. $res = $db
  43. ->where(['is_del' => $db::$is_del_normal, 'status' => $db::$status_normal, 'token' => $token])
  44. ->where('expire_time', '>=', date('Y-m-d H:i:s'))
  45. ->findOrEmpty()
  46. ->toArray();
  47. if (empty($res)) return json_show(1004, '该账号不存在');
  48. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  49. $list = SupplierRelationUserModel::field('id,supplierNo,supplierName')
  50. ->where(['is_del' => $db::$is_del_normal, 'uid' => $res['uid']])
  51. ->select()
  52. ->toArray();
  53. //获取这些供应商的状态
  54. $status = Db::name('supplier')
  55. ->where('is_del', 0)
  56. ->whereIn('code', array_column($list, 'supplierNo'))
  57. ->column('status', 'code');
  58. foreach ($list as &$value) {
  59. $value['status'] = $status[$value['supplierNo']];
  60. }
  61. $info = [
  62. 'uid' => $res['uid'],
  63. 'nickname' => $res['nickname'],
  64. 'mobile' => $res['mobile'],
  65. 'email' => $res['email'],
  66. 'token' => $res['token'],
  67. 'expire_time' => $res['expire_time'],
  68. 'supplier_list' => $list,
  69. ];
  70. return json_show(0, '获取用户信息成功', $info);
  71. }
  72. //获取供应商账号列表
  73. public static function getAccountList(array $param = [])
  74. {
  75. $db = new SupplierUserModel();
  76. $rs = $db
  77. ->where('is_del', $db::$is_del_normal);
  78. if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%');
  79. if ($param['status'] != '') $rs->where('status', $param['status']);
  80. if ($param['supplierNo'] != '') {
  81. $uids = SupplierRelationUserModel::where([
  82. 'is_del' => $db::$is_del_normal,
  83. 'supplierNo' => $param['supplierNo']
  84. ])->column('uid');
  85. $rs->whereIn('uid', $uids);
  86. }
  87. $count = $rs->count('uid');
  88. $list = $rs
  89. ->field('uid,nickname,mobile,email,status,addtime,creater')
  90. ->order('addtime', 'desc')
  91. ->page($param['page'], $param['size'])
  92. ->append(['supplier_list'])
  93. ->withAttr('supplier_list', function ($val, $data) use ($db) {
  94. return SupplierRelationUserModel::where([
  95. 'is_del' => $db::$is_del_normal,
  96. 'uid' => $data['uid']
  97. ])->field('supplierNo,supplierName,status')
  98. ->select()
  99. ->toArray();
  100. })
  101. ->select()
  102. ->toArray();
  103. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  104. }
  105. //修改供应商账号密码
  106. public static function changePassword(array $param = [])
  107. {
  108. $db = new SupplierUserModel();
  109. $res = $db
  110. ->where([
  111. 'uid' => $param['uid'],
  112. 'is_del' => $db::$is_del_normal,
  113. ])
  114. ->field('uid,status')
  115. ->findOrEmpty()
  116. ->toArray();
  117. if (empty($res)) return json_show(1004, '该账号不存在');
  118. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  119. //更新密码
  120. $password = get_encryption_password($param['password']);
  121. $rs = $db
  122. ->where('uid', $res['uid'])
  123. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  124. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  125. }
  126. //获取供应商账号账号信息
  127. public static function readAccount(array $param = [])
  128. {
  129. $res = SupplierUser::field('uid,nickname,mobile,email')
  130. ->where([
  131. 'uid' => $param['uid'],
  132. 'is_del' => SupplierUser::$is_del_normal,
  133. ])
  134. ->append(['supplier_list'])
  135. ->withAttr('supplier_list', function ($val, $data) {
  136. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  137. ->where([
  138. 'is_del' => SupplierUser::$is_del_normal,
  139. 'uid' => $data['uid']
  140. ])
  141. ->select()
  142. ->toArray();
  143. })
  144. ->findOrEmpty()
  145. ->toArray();
  146. return json_show(0, '获取账号信息成功', $res);
  147. }
  148. //添加供应商账号
  149. public static function addAccount(array $param = [], string $token = '')
  150. {
  151. $user = GetUserInfo($token);
  152. //查找供应商名称
  153. $supplierName = Db::name('supplier')
  154. ->whereIn('code', $param['supplierNo'])
  155. ->column('name', 'code');
  156. if (empty($supplierName)) return json_show(1004, '供应商不存在');
  157. Db::connect('mysql_sys')->startTrans();
  158. try {
  159. $db = new SupplierUserModel();
  160. $res = $db
  161. ->field('uid')
  162. ->where([
  163. 'is_del' => $db::$is_del_normal,
  164. 'mobile' => $param['mobile'],
  165. ])->findOrEmpty();
  166. if (!$res->isEmpty()) throw new Exception('该手机号已存在');
  167. //新增账号
  168. //默认密码存在app配置文件中,由于入口在admin应用中,所以如果将配置项放在abutment/config下面是读取不到的
  169. $password = get_encryption_password(Config::get('app.default_password'));
  170. $uid = $db->insertGetId([
  171. 'nickname' => $param['nickname'],//姓名
  172. 'mobile' => $param['mobile'],//手机号
  173. 'email' => $param['email'],//邮箱
  174. 'password' => $password['password'],//密码密文
  175. 'salt' => $password['salt'],//盐值
  176. 'status' => $db::$status_normal,
  177. 'is_del' => $db::$is_del_normal,
  178. 'createrid' => $user['data']['user_id'] ?? 0,
  179. 'creater' => $user['data']['nickname'] ?? '',
  180. ]);
  181. $insert_data = [];
  182. foreach ($param['supplierNo'] as $supplierNo) {
  183. $insert_data[] = [
  184. 'uid' => $uid,
  185. 'supplierNo' => $supplierNo,
  186. 'supplierName' => $supplierName[$supplierNo] ?? '',
  187. 'status' => $db::$status_normal,
  188. 'is_del' => $db::$is_del_normal,
  189. 'createrid' => $user['data']['user_id'] ?? 0,
  190. 'creater' => $user['data']['nickname'] ?? '',
  191. ];
  192. }
  193. if ($insert_data) Db::connect('mysql_sys')
  194. ->name('supplier_relation_user')
  195. ->insertAll($insert_data);
  196. Db::connect('mysql_sys')->commit();
  197. return json_show(0, '操作成功');
  198. } catch (Exception $exception) {
  199. Db::connect('mysql_sys')->rollback();
  200. return json_show(1005, '操作失败' . $exception->getMessage());
  201. }
  202. }
  203. //修改供应商账号
  204. public static function editAccount(array $param = [])
  205. {
  206. $user = GetUserInfo($param['token']);
  207. Db::connect('mysql_sys')->startTrans();
  208. try {
  209. $db = new SupplierUserModel();
  210. $res = $db
  211. ->field('uid,mobile')
  212. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  213. ->findOrEmpty()
  214. ->toArray();
  215. if (empty($res)) return json_show(1004, '该账号不存在');
  216. if ($res['mobile'] != $param['mobile']) {
  217. $temp = $db
  218. ->field('uid')
  219. ->where(['mobile' => $param['mobile'], 'is_del' => $db::$is_del_normal])
  220. ->where('uid', '<>', $param['uid'])
  221. ->findOrEmpty()
  222. ->isEmpty();
  223. if (!$temp) throw new Exception('要修改的手机号已存在');
  224. }
  225. $db
  226. ->where('uid', $param['uid'])
  227. ->strict(false)
  228. ->save($param);
  229. $relation_db = new SupplierRelationUserModel();
  230. $insert = $retain = [];
  231. foreach ($param['supplier_list'] as $supplier) {
  232. if (isset($supplier['id']) && $supplier['id'] != 0) $retain[] = $supplier['id'];
  233. else $insert[] = [
  234. 'uid' => $param['uid'],
  235. 'supplierNo' => $supplier['supplierNo'],
  236. 'supplierName' => $supplier['supplierName'],
  237. 'status' => $db::$status_normal,
  238. 'is_del' => $db::$is_del_normal,
  239. 'createrid' => $user['data']['user_id'] ?? 0,
  240. 'creater' => $user['data']['nickname'] ?? 0,
  241. ];
  242. }
  243. //除了保留id,其余删除
  244. $delete_where = [['is_del', '=', $db::$is_del_normal], ['uid', '=', $param['uid']]];
  245. if (!empty($retain)) $delete_where[] = ['id', 'not in', $retain];
  246. $relation_db->where($delete_where)->save(['is_del' => $db::$is_del_deleted]);
  247. if ($insert) $relation_db->insertAll($insert);
  248. Db::connect('mysql_sys')->commit();
  249. return json_show(0, '操作成功');
  250. } catch (Exception $exception) {
  251. Db::connect('mysql_sys')->rollback();
  252. return json_show(1005, '操作失败,' . $exception->getMessage());
  253. }
  254. }
  255. //修改供应商账号状态
  256. public static function statusAccount(array $param = [])
  257. {
  258. Db::connect('mysql_sys')->startTrans();
  259. try {
  260. $db = new SupplierUserModel();
  261. $res = $db
  262. ->field('uid,status')
  263. ->where([
  264. 'uid' => $param['uid'],
  265. 'is_del' => $db::$is_del_normal,
  266. ])->findOrEmpty();
  267. if ($res->isEmpty()) throw new Exception('该账号不存在');
  268. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  269. $db->where([
  270. 'uid' => $param['uid'],
  271. 'is_del' => $db::$is_del_normal,
  272. ])->where('status', '<>', $param['status'])
  273. ->save(['status' => $param['status']]);
  274. SupplierRelationUserModel::where([
  275. 'uid' => $param['uid'],
  276. 'is_del' => $db::$is_del_normal,
  277. ])->where('status', '<>', $param['status'])
  278. ->save(['status' => $param['status']]);
  279. Db::connect('mysql_sys')->commit();
  280. return json_show(0, '操作成功');
  281. } catch (Exception $exception) {
  282. Db::connect('mysql_sys')->rollback();
  283. return json_show(1005, '操作失败,' . $exception->getMessage());
  284. }
  285. }
  286. //删除供应商账号
  287. public static function deleteAccount(int $uid = 0)
  288. {
  289. Db::connect('mysql_sys')->startTrans();
  290. try {
  291. $db = new SupplierUserModel();
  292. $res = $db
  293. ->field('uid')
  294. ->where([
  295. 'uid' => $uid,
  296. 'is_del' => $db::$is_del_normal,
  297. ])->findOrEmpty();
  298. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  299. $db->where('uid', $uid)
  300. ->where('is_del', $db::$is_del_normal)
  301. ->save(['is_del' => $db::$is_del_deleted]);
  302. SupplierRelationUserModel::where('uid', $uid)
  303. ->where('is_del', $db::$is_del_normal)
  304. ->save(['is_del' => $db::$is_del_deleted]);
  305. Db::connect('mysql_sys')->commit();
  306. return json_show(0, '删除成功');
  307. } catch (Exception $exception) {
  308. Db::connect('mysql_sys')->rollback();
  309. return json_show(1005, '删除失败,' . $exception->getMessage());
  310. }
  311. }
  312. }