Wxapp.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\Wxapp as WxappModel;
  4. use app\admin\model\store\User as StoreUser;
  5. /**
  6. * 微信小程序模型
  7. * Class Wxapp
  8. * @package app\admin\model
  9. */
  10. class Wxapp extends WxappModel
  11. {
  12. /**
  13. * 获取小程序列表
  14. * @param boolean $is_recycle
  15. * @return \think\Paginator
  16. * @throws \think\exception\DbException
  17. */
  18. public function getList($is_recycle = false)
  19. {
  20. return $this->where('is_recycle', '=', (int)$is_recycle)
  21. ->where('is_delete', '=', 0)
  22. ->order(['create_time' => 'desc'])
  23. ->paginate(15, false, [
  24. 'query' => request()->request()
  25. ]);
  26. }
  27. /**
  28. * 从缓存中获取商城名称
  29. * @param $data
  30. * @return array
  31. */
  32. public function getStoreName($data)
  33. {
  34. $names = [];
  35. foreach ($data as $wxapp) {
  36. $names[$wxapp['wxapp_id']] = Setting::getItem('store', $wxapp['wxapp_id'])['name'];
  37. }
  38. return $names;
  39. }
  40. /**
  41. * 新增记录
  42. * @param $data
  43. * @return bool|mixed
  44. */
  45. public function add($data)
  46. {
  47. if ($data['password'] !== $data['password_confirm']) {
  48. $this->error = '确认密码不正确';
  49. return false;
  50. }
  51. if (StoreUser::checkExist($data['user_name'])) {
  52. $this->error = '商家用户名已存在';
  53. return false;
  54. }
  55. return $this->transaction(function () use ($data) {
  56. // 添加小程序记录
  57. $this->allowField(true)->save($data);
  58. // 商城默认设置
  59. (new Setting)->insertDefault($this['wxapp_id'], $data['store_name']);
  60. // 新增商家用户信息
  61. (new StoreUser)->add($this['wxapp_id'], $data);
  62. // 新增小程序默认帮助
  63. (new WxappHelp)->insertDefault($this['wxapp_id']);
  64. // 新增小程序diy配置
  65. (new WxappPage)->insertDefault($this['wxapp_id']);
  66. // 新增小程序分类页模板
  67. (new WxappCategory)->insertDefault($this['wxapp_id']);
  68. return true;
  69. });
  70. }
  71. /**
  72. * 移入移出回收站
  73. * @param bool $is_recycle
  74. * @return false|int
  75. */
  76. public function recycle($is_recycle = true)
  77. {
  78. return $this->save(['is_recycle' => (int)$is_recycle]);
  79. }
  80. /**
  81. * 软删除
  82. * @return false|int
  83. */
  84. public function setDelete()
  85. {
  86. return $this->transaction(function () {
  87. // 删除商家用户信息
  88. (new StoreUser)->setDelete($this['wxapp_id']);
  89. // 设置当前商城为已删除
  90. return $this->save(['is_delete' => 1]);
  91. });
  92. }
  93. }