ProductCode.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\store;
  8. use app\admin\controller\AuthController;
  9. use app\admin\model\user\User;
  10. use app\admin\model\user\User as UserModel;
  11. use crmeb\repositories\OrderRepository;
  12. use crmeb\repositories\ShortLetterRepositories;
  13. use crmeb\services\{ExpressService,
  14. JsonService,
  15. JsonService as Json,
  16. MiniProgramService,
  17. WechatService,
  18. FormBuilder as Form,
  19. CacheService,
  20. UtilService as Util};
  21. use app\admin\model\ump\StorePink;
  22. use crmeb\basic\BaseModel;
  23. use think\facade\Route as Url;
  24. use crmeb\services\YLYService;
  25. use think\facade\Log;
  26. use think\facade\Validate;
  27. /**
  28. * 订单管理控制器 同一个订单表放在一个控制器
  29. * Class StoreOrder
  30. * @package app\admin\controller\store
  31. */
  32. class ProductCode extends AuthController
  33. {
  34. /**
  35. * @return mixed
  36. */
  37. public function index($id)
  38. {
  39. $this->assign('id', $id);
  40. return $this->fetch();
  41. }
  42. public function list()
  43. {
  44. $where = Util::getMore([
  45. ['status', ''],
  46. ['bd', ''],
  47. ['page', 1],
  48. ['limit', 20],
  49. ['id']
  50. ]);
  51. return Json::successlayui(\app\admin\model\store\ProductCode::list($where));
  52. }
  53. /**
  54. * 显示创建资源表单页.
  55. *
  56. * @return \think\Response
  57. */
  58. public function code($id = 0)
  59. {
  60. $f = [];
  61. $f[] = Form::input('number', '生成数量');
  62. if ($id) $f[] = Form::hidden('id', $id);
  63. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  64. $this->assign(compact('form'));
  65. return $this->fetch('public/form-builder');
  66. }
  67. public function save()
  68. {
  69. $model = new \app\admin\model\store\ProductCode();
  70. $data = Util::postMore([
  71. 'number',
  72. 'id'
  73. ]);
  74. $validate = Validate::rule([
  75. 'number' => 'require',
  76. ]);
  77. $validate->message([
  78. 'number.require' => '数量不能为空',
  79. ]);
  80. if (!$validate->check($data)) {
  81. return Json::fail($validate->getError());
  82. }
  83. for ($i=1; $i <=$data['number']; $i++){
  84. $save[] = [
  85. 'product_id' => $data['id'],
  86. 'code' => generate_promotion_code(1),
  87. ];
  88. }
  89. $res = $model->saveAll($save);
  90. if ($res) return Json::successful('生成成功');
  91. return Json::fail('生成失败');
  92. }
  93. public function grant($id)
  94. {
  95. $f = [];
  96. $f[] = Form::select('uid', '绑定推荐人')->options(UserModel::field('uid as value,nickname as label')->select()->toArray())->filterable(true);
  97. $f[] = Form::hidden('id', $id);
  98. $form = Form::make_post_form('添加', $f, Url::buildUrl('bd_grant'));
  99. $this->assign(compact('form'));
  100. return $this->fetch('public/form-builder');
  101. }
  102. public function bd_grant()
  103. {
  104. $model = new \app\admin\model\store\ProductCode();
  105. $data = Util::postMore([
  106. 'uid',
  107. 'id'
  108. ]);
  109. $code = $model::where('id', 'in', $data['id'])->where('uid', '>', 0)->select()->toArray();
  110. if ($code){
  111. return Json::fail('兑换码已绑定');
  112. }
  113. $res = $model->where('id', 'in', $data['id'])->update(['uid' => $data['uid']]);
  114. if ($res) return Json::successful('绑定成功');
  115. return Json::fail('绑定失败');
  116. }
  117. /**
  118. * 显示创建资源表单页.
  119. *
  120. * @return \think\Response
  121. */
  122. public function edit($id = 0)
  123. {
  124. $data = \app\admin\model\many\Many::find($id);
  125. $f = [];
  126. $f[] = Form::input('name', '名称',$data->getData('name'))->col(12);
  127. $f[] = Form::frameImageOne('image', '封面图(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $data->getData('image'))->icon('image')->width('100%')->height('500px');
  128. $f[] = Form::input('money', '额度', $data->getData('money'));
  129. $f[] = Form::input('upper_limit', '参与上限', $data->getData('upper_limit'));
  130. $f[] = Form::dateTime('add_time', '开启时间', date('Y-m-d H:i:s', $data->getData('add_time')));
  131. $f[] = Form::dateTime('end_time', '结束时间', date('Y-m-d H:i:s', $data->getData('end_time')));
  132. $f[] = Form::input('sort', '排序',$data->getData('sort'));
  133. $f[] = Form::hidden('id', $id);
  134. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  135. $this->assign(compact('form'));
  136. return $this->fetch('public/form-builder');
  137. }
  138. /**
  139. * 修改
  140. * @return void
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function update()
  146. {
  147. $model = new \app\admin\model\many\Many();
  148. $data = Util::postMore([
  149. 'id',
  150. 'name',
  151. 'image',
  152. 'money',
  153. 'upper_limit',
  154. 'add_time',
  155. 'end_time',
  156. 'sort',
  157. ]);
  158. $validate = Validate::rule('name', 'require')->rule([
  159. 'name' => 'require',
  160. 'image' => 'require',
  161. 'money' => 'require',
  162. 'upper_limit' => 'require',
  163. 'add_time' => 'require',
  164. 'end_time' => 'require',
  165. ]);
  166. $validate->message([
  167. 'name.require' => '名称不能为空',
  168. 'image.require' => '图片不能为空',
  169. 'money.require' => '额度不能为空',
  170. 'upper_limit.require' => '上限不能为空',
  171. 'add_time.require' => '请选择进场时间',
  172. 'end_time.require' => '请选择结束时间',
  173. ]);
  174. if (!$validate->check($data)) {
  175. return Json::fail($validate->getError());
  176. }
  177. $details = $model->find($data['id']);
  178. $details['name'] = $data['name'];
  179. $details['image'] = $data['image'];
  180. $details['money'] = $data['money'];
  181. $details['upper_limit'] = $data['upper_limit'];
  182. $details['add_time'] = strtotime($data['add_time']);
  183. $details['end_time'] = strtotime($data['end_time']);
  184. $details['sort'] = $data['sort'];
  185. $res = $details->save();
  186. if ($res) return Json::successful('修改成功');
  187. return Json::fail('修改失败');
  188. }
  189. /**
  190. * 显示创建资源表单页.
  191. *
  192. * @return \think\Response
  193. */
  194. public function next($id = 0)
  195. {
  196. $f = [];
  197. $f[] = Form::dateTime('add_time', '开启时间');
  198. $f[] = Form::dateTime('end_time', '结束时间');
  199. $f[] = Form::hidden('id', $id);
  200. $form = Form::make_post_form('添加', $f, Url::buildUrl('next_save'));
  201. $this->assign(compact('form'));
  202. return $this->fetch('public/form-builder');
  203. }
  204. public function next_save()
  205. {
  206. $model = new \app\admin\model\many\Many();
  207. $data = Util::postMore([
  208. 'id',
  209. 'add_time',
  210. 'end_time',
  211. ]);
  212. $validate = Validate::rule([
  213. 'add_time' => 'require',
  214. 'end_time' => 'require',
  215. ]);
  216. $validate->message([
  217. 'add_time.require' => '请选择开启时间',
  218. 'end_time.require' => '请选择结束时间',
  219. ]);
  220. if (!$validate->check($data)) {
  221. return Json::fail($validate->getError());
  222. }
  223. $details = $model->find($data['id']);
  224. if ($details['status'] == 1) return Json::fail('未结束不能进行下一场');
  225. if ($details['suc'] == 2) return Json::fail('已众筹失败无法就行下一场');
  226. if (strtotime($data['end_time']) < strtotime($data['add_time'])) return Json::fail('结束时间不能小于开启时间');
  227. $details['stage'] += 1;
  228. $details['status'] = 1;
  229. $details['number'] = 0;//重置额度
  230. $details['money'] = intval($details['money'] * 1.3);//提搞额度130%
  231. $details['suc'] = 0;
  232. $details['add_time'] = strtotime($data['add_time']);
  233. $details['end_time'] = strtotime($data['end_time']);
  234. $res = $details->save();
  235. if ($res) return Json::successful('成功');
  236. return Json::fail('失败');
  237. }
  238. /**
  239. * 删除
  240. * @param $id
  241. * @return void
  242. * @throws \Exception
  243. */
  244. public function delete($id)
  245. {
  246. if (!$id) Json::fail('删除失败');
  247. $model = new \app\admin\model\store\ProductCode();
  248. $res = $model->where('id', $id)->delete();
  249. if ($res){
  250. return Json::success('删除成功!');
  251. }else{
  252. return Json::fail($model->getErrorInfo());
  253. }
  254. }
  255. }