App.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\admin\model\app;
  3. use app\admin\model\page\Page as PageModel;
  4. use app\admin\model\Shop as ShopUser;
  5. use app\common\model\app\App as AppModel;
  6. use app\admin\model\user\Grade as GradeModel;
  7. class App extends AppModel
  8. {
  9. /**
  10. * 获取小程序列表
  11. */
  12. public function getList($limit, $is_recycle = false)
  13. {
  14. return $this->alias('app')->field(['app.*,user.user_name'])->where('is_recycle', '=', (int)$is_recycle)
  15. ->join('shop_user user', 'user.app_id = app.app_id','left')
  16. ->where('user.is_super', '=', 1)
  17. ->where('app.is_delete', '=', 0)
  18. ->order(['create_time' => 'asc'])
  19. ->paginate($limit);
  20. }
  21. /**
  22. * 新增记录
  23. */
  24. public function add($data)
  25. {
  26. if ($data['password'] !== $data['password_confirm']) {
  27. $this->error = '确认密码不正确';
  28. return false;
  29. }
  30. if (ShopUser::checkExist($data['user_name'])) {
  31. $this->error = '商家用户名已存在';
  32. return false;
  33. }
  34. $this->startTrans();
  35. try {
  36. // 添加小程序记录
  37. $this->save($data);
  38. // 新增商家用户信息
  39. $ShopUser = new ShopUser;
  40. if (!$ShopUser->add($this['app_id'], $data)) {
  41. $this->error = $ShopUser->error;
  42. return false;
  43. }
  44. // 新增应用diy配置
  45. (new PageModel)->insertDefault($this['app_id']);
  46. // 默认等级
  47. (new GradeModel)->insertDefault($this['app_id']);
  48. $this->commit();
  49. return true;
  50. } catch (\Exception $e) {
  51. $this->error = $e->getMessage();
  52. $this->rollback();
  53. return false;
  54. }
  55. }
  56. /**
  57. * 修改记录
  58. */
  59. public function edit($data)
  60. {
  61. $this->startTrans();
  62. try {
  63. $save_data = [
  64. 'app_name' => $data['app_name'],
  65. ];
  66. $this->save($save_data);
  67. $user_data = [
  68. 'user_name' => $data['user_name']
  69. ];
  70. if (!empty($data['password'])) {
  71. $user_data['password'] = salt_hash($data['password']);
  72. }
  73. $shop_user = (new ShopUser())->where('app_id', '=', $this['app_id'])->where('is_super', '=', 1)->find();
  74. if($shop_user['user_name'] != $data['user_name']){
  75. if (ShopUser::checkExist($data['user_name'])) {
  76. $this->error = '商家用户名已存在';
  77. return false;
  78. }
  79. }
  80. $shop_user->save($user_data);
  81. $this->commit();
  82. return true;
  83. } catch (\Exception $e) {
  84. $this->error = $e->getMessage();
  85. $this->rollback();
  86. return false;
  87. }
  88. }
  89. /**
  90. * 移入移出回收站
  91. */
  92. public function recycle($is_recycle = true)
  93. {
  94. return $this->save(['is_recycle' => (int)$is_recycle]);
  95. }
  96. /**
  97. * 软删除
  98. */
  99. public function setDelete()
  100. {
  101. return $this->save(['is_delete' => 1]);
  102. }
  103. }