OrderController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * 订单管理
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-03 16:51:03
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AlbumManufacturerModel;
  12. use App\Models\AlbumOrderModel;
  13. use App\Models\AlbumPartsModel;
  14. use App\Models\AlbumProgressModel;
  15. use App\Models\AlbumUserModel;
  16. use App\Models\FurnitureFormidModel;
  17. use App\Services\Base\Attachment;
  18. use EasyWeChat\Factory;
  19. use EasyWeChat\Payment\Order;
  20. use Image;
  21. use Illuminate\Http\Request;
  22. use App\Repositories\Base\Criteria\OrderBy;
  23. use App\Repositories\Album\Criteria\MultiWhere;
  24. use App\Repositories\Album\OrderRepository;
  25. use Illuminate\Support\Facades\DB;
  26. class OrderController extends Controller
  27. {
  28. private $repository;
  29. public function __construct(OrderRepository $repository) {
  30. if(!$this->repository) $this->repository = $repository;
  31. parent::__construct();
  32. }
  33. function index(Request $request) {
  34. $search['keyword'] = $request->input('keyword');
  35. $search['status'] = $request->input('status');
  36. $search['expected'] = $request->input('expected');
  37. $search['start'] = $request->input('start');
  38. $search['end'] = $request->input('end');
  39. $search['storeid'] = $this->getStoreId();
  40. $order = array();
  41. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  42. $order[$request['sort_field']] = $request['sort_field_by'];
  43. }else{
  44. $order['id']='DESC';
  45. }
  46. $list = $this->repository->searchOrder($search,$order);
  47. return view('admin.album.order.index',compact('list'));
  48. }
  49. function check(Request $request) {
  50. $data = $this->repository->find(request('id'));
  51. $img = explode(',',$data['picture']);
  52. $parts = AlbumPartsModel::where('store_id', $this->getStoreId())->where('order_id', request('id'))
  53. ->get(['name', 'count']);
  54. return view('admin.album.order.check',compact('data','img','parts'));
  55. }
  56. /**
  57. * 添加
  58. *
  59. */
  60. public function create(Request $request)
  61. {
  62. if($request->method() == 'POST') {
  63. return $this->_createSave();
  64. }
  65. return view('admin.album.order.edit');
  66. }
  67. /**
  68. * 保存修改
  69. */
  70. private function _createSave(){
  71. $data = (array) request('data');
  72. $data['store_id'] = $this->getStoreId();
  73. $array_pic = request('picture');
  74. if ($array_pic && isset($array_pic['url'])) {
  75. $data['picture'] = '';
  76. foreach ($array_pic['url'] as $item) {
  77. $data['picture'] .= $item . ',';
  78. }
  79. if ($data['picture']) $data['picture'] = rtrim($data['picture'], ',');
  80. } else {
  81. $data['picture'] = '';
  82. }
  83. $data['status'] = 0;
  84. $data['sno'] = date('YmdHmis');
  85. $res = $this->repository->create($data);
  86. if($res) {
  87. $this->getQrcode($this->getStoreId(),$res->id);
  88. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  89. $url[] = array('url'=>U( 'Album/Order/create'),'title'=>'继续添加');
  90. $this->showMessage('添加成功',$url);
  91. }else{
  92. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  93. return $this->showWarning('添加失败',$url);
  94. }
  95. }
  96. /**
  97. *
  98. * 修改
  99. *
  100. *
  101. */
  102. public function update(Request $request) {
  103. if($request->method() == 'POST') {
  104. return $this->_updateSave();
  105. }
  106. $data = $this->repository->find($request->get('id'));
  107. $data['picture'] = explode(',',$data['picture']);
  108. return view('admin.album.order.edit',compact('data'));
  109. }
  110. /**
  111. * 保存修改
  112. */
  113. private function _updateSave() {
  114. $data = (array) request('data');
  115. $array_pic = request('picture');
  116. if ($array_pic && isset($array_pic['url'])) {
  117. $data['picture'] = '';
  118. foreach ($array_pic['url'] as $item) {
  119. $data['picture'] .= $item . ',';
  120. }
  121. if ($data['picture']) $data['picture'] = rtrim($data['picture'], ',');
  122. } else {
  123. $data['picture'] = '';
  124. }
  125. $old = $this->repository->find(request('id'))->picture;
  126. $ok = $this->repository->update(request('id'),$data);
  127. if($ok) {
  128. //操作成功,删除原来的图片
  129. if ($old) {
  130. $pics = explode(',', $old);
  131. foreach ($pics as $pic) {
  132. if (!in_array($pic, $array_pic['url'])) {
  133. $md5 = $this->getarea($pic);
  134. $attache = new Attachment();
  135. $attache->deleteAttachment($md5);
  136. }
  137. }
  138. }
  139. return $this->showMessage('操作成功',urldecode(request('_referer')));
  140. }else{
  141. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  142. return $this->showWarning('操作失败',$url);
  143. }
  144. }
  145. public function view(Request $request) {
  146. $data = $this->repository->find(request('id'));
  147. $img = explode(',',$data['picture']);
  148. $this->getQrcode($this->getStoreId(),request('id'));
  149. return view('admin.album.order.view',compact('data','img'));
  150. }
  151. public function picture(Request $request) {
  152. $data = $this->repository->find(request('id'));
  153. $img = explode(',',$data['picture']);
  154. return view('admin.album.order.picture',compact('img'));
  155. }
  156. public function addecomment(Request $request){
  157. $data = $this->repository->find(request('id'));
  158. $data->expected_comment = $request->get('expected_comment');
  159. $ok = $data->save();
  160. if($ok) {
  161. return $this->showMessage('操作成功');
  162. }else{
  163. return $this->showWarning('操作失败');
  164. }
  165. }
  166. /**
  167. *
  168. * 状态改变
  169. *
  170. */
  171. public function status(Request $request) {
  172. $data = [
  173. 'status'=>request('status'),
  174. 'price' =>request('price'),
  175. 'verifier_id' =>$this->_user->id,
  176. 'expected_time' =>request('expected_time'),
  177. ];
  178. $ok = $this->repository->update(request('id'),$data);
  179. if($ok) {
  180. foreach (request('name') as $k =>$v){
  181. $parts[$k]['name'] = $v;
  182. $parts[$k]['count'] = request('count')[$k];
  183. $parts[$k]['order_id'] = request('id');
  184. $parts[$k]['store_id'] = $this->getStoreId();
  185. if($parts[$k]['name']){
  186. AlbumPartsModel::create($parts[$k]);
  187. }
  188. }
  189. $data = [
  190. 'status' =>request('status'),
  191. 'order_id' =>request('id'),
  192. 'store_id' =>$this->getStoreId(),
  193. 'description' => '订单已审核,修理费为'.request('price').'订单下发到生产部,等待制作'
  194. ];
  195. AlbumProgressModel::create($data);
  196. $storeid =$this->getStoreId();
  197. $setting = AlbumManufacturerModel::where('store_id', $storeid)->first();
  198. $config = [
  199. 'app_id' => $setting->service_app_id,
  200. 'secret' => $setting->service_app_secret,
  201. // 下面为可选项
  202. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  203. 'response_type' => 'array',
  204. ];
  205. $app = Factory::miniProgram($config);
  206. $users = AlbumUserModel::where('store_id',1)->where('role',1)->get();
  207. // foreach ($users as $user){
  208. // $formid = FurnitureFormidModel::where('store_id',$storeid)->where(DB::raw("DATEDIFF(NOW(),created_at)"), '<', 7)->where('openid',$user->wechat_open_id)->where('status',0)->orderBy('created_at','asc')->first();
  209. // $app->template_message->send([
  210. // 'touser' => $user->wechat_open_id,
  211. // 'template_id' => '',
  212. // 'page' => 'index',
  213. // 'form_id' => $formid->formid,
  214. // 'data' => [
  215. // 'keyword1' => 'VALUE',
  216. // 'keyword2' => 'VALUE2',
  217. // // ...
  218. // ],
  219. // ]);
  220. // }
  221. return $this->showMessage('操作成功');
  222. }else{
  223. return $this->showWarning('操作失败');
  224. }
  225. }
  226. /**
  227. * 删除
  228. */
  229. public function destroy(Request $request) {
  230. $bool = $this->repository->destroy($request->get('id'));
  231. if($bool) {
  232. return $this->showMessage('操作成功');
  233. }else{
  234. return $this->showWarning("操作失败");
  235. }
  236. }
  237. public function getPic(Request $request){
  238. $order = AlbumOrderModel::find(request('id'));
  239. $img = $order->picture;
  240. $img = explode(',',$img);
  241. return json_encode($img);
  242. }
  243. public function getQrcode($store_id, $id)
  244. {
  245. $this->wechat_app = AlbumManufacturerModel::where('store_id', $store_id)->first();
  246. $config = [
  247. 'app_id' => $this->wechat_app->service_app_id,
  248. 'secret' => $this->wechat_app->service_app_secret,
  249. // 下面为可选项
  250. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  251. 'response_type' => 'array',
  252. ];
  253. $app = Factory::miniProgram($config);
  254. $response = $app->app_code->getQrCode('pages/search/search?order_id=' . $id,300);
  255. $path = public_path() . '/download';
  256. $qrcode = $response->save($path, $id);
  257. if ($qrcode) {
  258. $qrcode = env('APP_URL') . '/download/' . $id . '.jpg';
  259. $order = AlbumOrderModel::find($id);
  260. $order->qrcode = $qrcode;
  261. $order->save();
  262. return true;
  263. } else {
  264. return false;
  265. }
  266. }
  267. public function getarea($str)
  268. {
  269. $start = strripos($str, '/');
  270. $first = substr($str, $start + 1);
  271. $end = strripos($first, '.');
  272. $last = substr($first, 0, $end);
  273. return $last;
  274. }
  275. }